package 二维数组拐弯走;

import org.junit.Test;

import static MyUtils.MHUtils.*;

/*
靠xmind拆分
 */
public class Z字形变换6 {
    @Test
    public void test() {
        String s = "PAYPALISHIRING";
        s="AB";
        System.out.println(convert(s,1));
    }
    public String convert(String s, int numRows) {
        char[] chars = s.toCharArray();
        //int/4永远向下取整 -> 要想有小数+取整上ceil下floor必须double化先
        int cols = (int) (Math.ceil((double) s.length()/4)*2);
        char[][] g = new char[numRows][cols];
        //0下 1右上
        int f=0;
        int i=0,j=0;
        //特：就一行时：不满足上来就往下走
        if(numRows==1){
            return s;
        }
        for (int k = 0; k < s.length(); k++) {
            //cur
            g[i][j] = chars[k];
            ptCharShuzu(g);
            //迭代
            if(f==0){
                if(i+1<g.length) {
                    i++;
                }
                else {
                    f = 1;
                    //因为k每轮必++,不可重复覆盖
                    i--;j++;
                }
            }
            else if(f==1){
                if(i-1>=0){
                    i--;j++;
                }
                else {
                    f=0;
                    i++;
                }
            }
        }
        //按行打印
        StringBuilder sb = new StringBuilder();
        for (int k = 0; k < g.length; k++) {
            for (int h = 0; h < g[0].length; h++) {
                //char[][]默认=0
                if(g[k][h]!=0){
                    sb.append(g[k][h]);
                }
            }
        }
        return sb.toString();
    }
}
